package vue;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.filechooser.FileNameExtensionFilter;

import controleur.InfoListener;
import controleur.SaveListener;

public class ListContact extends JFrame implements ListInterface{

	private JList list;
	private JButton save, modify;
	private JTextField t1, t2, t3, t4, t5, t6, t7, t8;
	private JLabel lblphoto;
	private String file;
	private JComboBox cbGroup;

	/**
	 * Constructor
	 * @param t the list of contacts
	 */
	public ListContact(String[] t) {
		// TODO Auto-generated constructor stub
		this.setSize(500, 600);
		this.setTitle(AddressBook.LISTOFCONTACTS);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		JPanel pan = new JPanel();
		list = new JList(t);
		init(pan);
		this.setContentPane(pan);
		this.setVisible(true);
		file = new String("");
	}

	/**
	 * initialize graphic components
	 * @param pan
	 */
	private void init(JPanel pan) {
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setPreferredSize(new Dimension(200, 500));
		JPanel p1 = new JPanel();
		p1.add(list);
		JPanel p2 = new JPanel();
		p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));

		t1 = new JTextField();
		t1.setPreferredSize(new Dimension(150, 30));
		t2 = new JTextField();
		t2.setPreferredSize(new Dimension(150, 30));
		t3 = new JTextField();
		t3.setPreferredSize(new Dimension(150, 30));
		t4 = new JTextField();
		t4.setPreferredSize(new Dimension(150, 30));
		t5 = new JTextField();
		t5.setPreferredSize(new Dimension(150, 30));
		t6 = new JTextField();
		t6.setPreferredSize(new Dimension(150, 30));
		t7 = new JTextField();
		t7.setPreferredSize(new Dimension(150, 30));
		t8 = new JTextField();
		t8.setPreferredSize(new Dimension(150, 30));

		lblphoto = new JLabel(new ImageIcon("./images/nophoto.png"));
		lblphoto.setPreferredSize(new Dimension(150, 200));
		cbGroup = new JComboBox();
		cbGroup.addItem(AddressBook.FAMILY);
		cbGroup.addItem(AddressBook.FRIEND);
		cbGroup.addItem(AddressBook.WORK);
		cbGroup.setPreferredSize(new Dimension(130, 20));

		t1.setEditable(false);
		t2.setEditable(false);
		t3.setEditable(false);
		t4.setEditable(false);
		t5.setEditable(false);
		t6.setEditable(false);
		t7.setEditable(false);
		t8.setEditable(false);
		cbGroup.setEnabled(false);

		t7.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (!save.isEnabled() && !t7.getText().equals("")) {
					Desktop d = Desktop.getDesktop();
					try {
						d.browse(new URI(t7.getText()));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});

		t8.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (!save.isEnabled() && !t8.getText().equals("")) {
					Desktop d = Desktop.getDesktop();
					try {
						URI uriMailTo = new URI("mailto:" + t8.getText());
						d.mail(uriMailTo);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});

		lblphoto.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (save.isEnabled()) {
					JFileChooser chooser = new JFileChooser();
					FileNameExtensionFilter filter = new FileNameExtensionFilter(
							"JPG", "jpg");
					chooser.setFileFilter(filter);
					int returnVal = chooser.showOpenDialog(null);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						file = chooser.getSelectedFile().getPath();
					}
				}
			}
		});

		list.addListSelectionListener(new InfoListener(t1, t2, t3, t4,
				t5, t6, t7, t8, cbGroup, list, lblphoto));

		p2.add(lblphoto);
		p2.add(t1);
		p2.add(t2);
		p2.add(t3);
		p2.add(t4);
		p2.add(t5);
		p2.add(t6);
		p2.add(t7);
		p2.add(t8);
		p2.add(cbGroup);

		modify = new JButton(AddressBook.MODIFY);
		modify.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				modify.setEnabled(false);
				save.setEnabled(true);
				t3.setEditable(true);
				t4.setEditable(true);
				t5.setEditable(true);
				t6.setEditable(true);
				t7.setEditable(true);
				t8.setEditable(true);
				cbGroup.setEnabled(true);
			}
		});

		save = new JButton(AddressBook.SAVEUPDATES);
		save.setEnabled(false);
		save.addActionListener(new SaveListener(t1, t2, t3, t4, t5, t6,
				t7, t8, cbGroup, this));

		p2.add(modify);
		p2.add(save);
		pan.add(p1);
		pan.add(p2);

	}

	public String getFile() {
		return file;
	}

	public JFrame getFrame() {
		return this;
	}
}
